package itqf.service;



import itqf.dao.CartDaoImpl;
import itqf.dao.impl.CartDao;
import itqf.entity.Cart;
import itqf.service.impl.CartService;

import java.math.BigDecimal;
import java.util.List;

/**
 * com.itqf.service
 * 2020/9/16
 */
public class CartServiceImpl implements CartService {
    CartDao cartDao = new CartDaoImpl();
    @Override
    public int addCart(Cart cart) {
        Cart ucart = cartDao.findCartByUidAndPid(cart.getUid(),cart.getPid());
        int i = 0;
        if(ucart==null){
           i =  cartDao.addCart(cart);
        }else{
            ucart.setCnum(ucart.getCnum()+1);
            //  BigDecimal price = ucart.getCcount();
            BigDecimal price = cart.getCcount();
            BigDecimal allprice = price.multiply(new BigDecimal(ucart.getCnum()));
            ucart.setCcount(allprice);
            i =  cartDao.update(ucart);
        }
        return i;
    }

    @Override
    public List<Cart> findAllCart(int uid) {
        return cartDao.findAllCart(uid);
    }

    @Override
    public int mupdateNum(int pid, int uid,BigDecimal price) {
        Cart cart = cartDao.findCartByUidAndPid(uid,pid);
        cart.setCnum(cart.getCnum()-1);
        BigDecimal bigDecimal = price;
        bigDecimal = bigDecimal.multiply(new BigDecimal(cart.getCnum()));
        cart.setCcount(bigDecimal);
        int count = cartDao.update(cart);
        return count;
    }


    @Override
    public int pupdateNum(int pid, int uid,BigDecimal price) {
        Cart cart = cartDao.findCartByUidAndPid(uid,pid);
        cart.setCnum(cart.getCnum()+1);
        BigDecimal bigDecimal = price;
        bigDecimal = bigDecimal.multiply(new BigDecimal(cart.getCnum()));
        cart.setCcount(bigDecimal);
        int count = cartDao.update(cart);
        return count;
    }

    @Override
    public int delCart(int uid, int pid) {
        return cartDao.del(uid,pid);
    }


}
